package a51qindian.com.comprehensive.net;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;

/**
 * Author: Li
 * Date: 2017/10/19 0019
 * Description:
 */

public abstract class StringObserver<T> implements Observer<T> {
    @Override
    public void onNext(@NonNull T t) {
        if (t instanceof String) {
            String s= (String) t;
            try {
                JSONObject object=new JSONObject(s);
                int code=object.getInt("code");
                String info=object.getString("info");
                showInfo(info);
                if (code==1) {
                    onSuccessCode();
                }else {
                    onErrorCode();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onComplete() {

    }

    protected abstract void showInfo(String info);
    protected abstract void onErrorCode();
    protected abstract void onSuccessCode();
}
